//
//  KFDAlarmSettingsViewController.m
//  KFD
//
//  Created by JIRENTIANXIANG on 7/24/15.
//  Copyright © 2015 kfd. All rights reserved.
//

#import "KFDAlarmSettingsViewController.h"
#import "KFDSettingsManager.h"
#import "JTToast.h"
#import "KFDUIConfigurator.h"
#import "KFDAccountManager.h"
#import "UIAlertController+KFD.h"

@interface KFDAlarmSettingsViewController ()

@property (strong,nonatomic) KFDSettingsManager* settingsManager;

@property (weak, nonatomic) IBOutlet UITextField *basePriceField;
@property (weak, nonatomic) IBOutlet UITextField *floatPriceField;
@property (weak, nonatomic) IBOutlet UITextField *lowestPointField;
@property (weak, nonatomic) IBOutlet UITextField *highestPointField;
@property (weak, nonatomic) IBOutlet UISwitch *floatAlarmSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *pointAlarmSwitch;

@end

@implementation KFDAlarmSettingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    KFDUIConfigurator* uiConfigurator=[KFDUIConfigurator configuratorForViewController:self];
    [uiConfigurator configureNavigationBarWithBackAction:@selector(goBack)];
    self.title=@"预警设置";
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"header-sound-ico"] style:UIBarButtonItemStylePlain target:self action:@selector(ringtoneButtonPressed)];
    
    self.basePriceField.keyboardType=UIKeyboardTypeDecimalPad;
    self.floatPriceField.keyboardType=UIKeyboardTypeDecimalPad;
    self.lowestPointField.keyboardType=UIKeyboardTypeDecimalPad;
    self.highestPointField.keyboardType=UIKeyboardTypeDecimalPad;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.settingsManager=[KFDSettingsManager sharedManager];
    [self.settingsManager getAlarmSettingsWithType:self.type successHandler:^(KFDAlarmSettings *settings) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.basePriceField.text=settings.base_price?[NSString stringWithFormat:@"%f",settings.base_price.floatValue]:@"";
            self.floatPriceField.text=settings.float_price?[NSString stringWithFormat:@"%f",settings.float_price.floatValue]:@"";
            self.lowestPointField.text=settings.point_min?[NSString stringWithFormat:@"%f",settings.point_min.floatValue]:@"";
            self.highestPointField.text=settings.point_max?[NSString stringWithFormat:@"%f",settings.point_max.floatValue]:@"";
            self.floatAlarmSwitch.on=settings.isfloat;
            self.pointAlarmSwitch.on=settings.ispoint;
        });
    } failureHandler:^(KFDResponseBody *response) {
        
    }];
}

-(void)ringtoneButtonPressed
{
    UIViewController* ringtoneSettingsVC=[[UIStoryboard storyboardWithName:@"KFDAlarmRingtoneSettings" bundle:nil] instantiateViewControllerWithIdentifier:@"KFDAlarmRingtoneSettingsViewController"];
    [self.navigationController pushViewController:ringtoneSettingsVC animated:YES];
}

-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)floatAlarmSwitch:(id)sender {
    
}

- (IBAction)pointAlarmSwitch:(id)sender {
    
}

- (IBAction)saveButtonPressed:(id)sender {
    NSString* basePrice=self.basePriceField.text;
    NSString* floatPrice=self.floatPriceField.text;
    NSString* minPrice=self.lowestPointField.text;
    NSString* maxPrice=self.highestPointField.text;
    KFDSettingsManager* settingsManager=[KFDSettingsManager sharedManager];
    __block BOOL passed=YES;
    if (self.floatAlarmSwitch.isOn) {
        [settingsManager checkAlarmBaseValue:basePrice floatValue:floatPrice illegalInputHandler:^(NSString *message) {
            [[JTToast makeText:message] showInView:self.view size:CGSizeMake(message.length*20, 40)];
            passed=NO;
        }];
    }
    if (self.pointAlarmSwitch.isOn) {
        [settingsManager checkMinValue:minPrice maxValue:maxPrice illegalInputHandler:^(NSString *message) {
            [[JTToast makeText:message] showInView:self.view size:CGSizeMake(message.length*20, 40)];
            passed=NO;
        }];
    }
    if (passed) {
        KFDAlarmSettings* settings=settingsManager.alarmSettingsOfAllTypes[self.type];
        settings.typename=self.type;
        settings.base_price=@(self.basePriceField.text.floatValue);
        settings.float_price=@(self.floatPriceField.text.floatValue);
        settings.point_min=@(self.lowestPointField.text.floatValue);
        settings.point_max=@(self.highestPointField.text.floatValue);
        settings.isfloat=self.floatAlarmSwitch.isOn;
        settings.ispoint=self.pointAlarmSwitch.isOn;
        [self.settingsManager updateAlarmSettingsWithType:self.type successHandler:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [[JTToast makeText:@"保存成功"] showInView:self.view size:CGSizeMake(80, 40)];
            });
        } failureHandler:^(KFDResponseBody *response) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [[JTToast makeText:response.message] showInView:self.view size:CGSizeMake(response.message.length*20, 40)];
            });
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
